package com.infobie.android.pocketdhamma.dhammapada;

import com.infobie.android.pocketdhamma.R;
import com.infobie.android.pocketdhamma.dhammapada.DB.*;

import gueei.binding.Command;
import gueei.binding.Observable;
import gueei.binding.app.BindingActivity;
import gueei.binding.cursor.CursorObservable;
import gueei.binding.observables.StringObservable;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class VerseList extends BindingActivity {
	DBHelper dbHelper = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    //setContentView(R.layout.verse_list); // default
        this.setAndBindRootView(R.layout.verse_list, this);
        
        try
        {
        	Intent intent = this.getIntent();
        	String chapterName = intent.getStringExtra("ChapterName");
        	long chapterId = intent.getLongExtra("ChapterId", 0);
        	if (chapterId > 0)
        		this.ChapterDescription.set("Chapter " + chapterId + ". " + chapterName);
	        dbHelper = new DBHelper(this);
	        Cursor c = dbHelper.GetVersesByChapterId((int)chapterId);
	        VerseList.setCursor(c);
        }
        catch(Exception ex)
        {
        	Alerts.Show(this, "Error", ex.getMessage());
        }
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		dbHelper.close();
	}

	public final StringObservable ChapterDescription = new StringObservable("");
	public final CursorObservable<VerseItem> VerseList = new CursorObservable<VerseItem>(VerseItem.class);
    public final Observable<Object> VerseItemClicked = new Observable<Object>(Object.class);
    public final Command VerseItemClickedCommand = new Command(){
            public void Invoke(View view, Object... args) {
            	VerseItem verse = (VerseItem)VerseItemClicked.get();
            	String sText = "Verse " + verse.VerseId.get().toString() + ". " + verse.VerseName.get();
                Toast.makeText(view.getContext(), sText, Toast.LENGTH_SHORT).show();
                
                Intent intent = new Intent(VerseList.this, VerseView.class);
                intent.putExtra("ChapterId", verse.ChapterId.get());
                intent.putExtra("ChapterDescription", ChapterDescription.get());
                intent.putExtra("VerseId", verse.VerseId.get());
                VerseList.this.startActivity(intent);
            }
    };
}
